const {ipcRenderer} = require('electron');
const constValue = require('./ConstValue.js');
//引入vue
const {createApp, ref, reactive, onMounted} = Vue;


const app = createApp({
    setup() {
        const cpData = reactive({
            arr: []
        });

        /**
         * 页面加载完成
         */
        onMounted(() => {
            //开始监听
            monitorDisplay();
        })



        /**
         * 清空历史的点击事件
         */
        function clearHistory() {
            //清理已有历史记录
            ipcRenderer.invoke(constValue.EVENT_CLEAR_CLIP_BOARD_IPC).then(res => {
                if (res === 'success') {
                    alert("清理成功")
                } else {
                    alert("啊哦,未知错误!");
                }
            });
        }

        /**
         * 复制一个
         * @param index
         */
        function copyOne(index) {
            ipcRenderer.invoke(constValue.EVENT_COPY_ONE_IPC, index);
        }

        /**
         * 监听剪切板内容
         */
        function monitorDisplay(){
            let clipJsonStr = "";
            let interval = setInterval(() => {
                ipcRenderer.invoke(constValue.CLIP_BOARD_KEY_FOR_IPC).then((res) => {
                    if (res !== clipJsonStr) {
                        clipJsonStr = res;
                        //给赋值
                        cpData.arr = JSON.parse(clipJsonStr);
                    }
                })

            }, constValue.REFRESH_DELAY);
        }


        return {
            cpData,
            clearHistory,
            copyOne
        }
    }
});
app.mount('#app')